package com.yuhj.ontheway.clients;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yuhj.ontheway.bean.JingXuanData;
import com.yuhj.ontheway.bean.UserInfo;

import android.R.integer;
import android.provider.MediaStore.Video;
import android.util.Log;

/**
 * @name ClientApi
 * @Descripation 这是一个用来访问网络的类<br>
 *		1、<br>
 *		2、<br>
 *      3、<br>
 * @author 禹慧军
 * @date 2014-10-22
 * @version 1.0
 */
public class ClientApi {
	

	public ClientApi() {
		// TODO Auto-generated constructor stub
	}

	public static JSONObject ParseJson(final String path, final String encode) {
		// TODO Auto-generated method stub
		HttpClient httpClient = new DefaultHttpClient();
		HttpParams httpParams = httpClient.getParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
		HttpConnectionParams.setSoTimeout(httpParams, 10000);
		HttpPost httpPost = new HttpPost(path);
		try {
			HttpResponse httpResponse = httpClient.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				String result = EntityUtils.toString(httpResponse.getEntity(),
						encode);
				
				JSONObject jsonObject = new JSONObject(result);
				return jsonObject;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			return null;

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			if (httpClient != null)
				httpClient.getConnectionManager().shutdown();
		}
		return null;

	}

	public static ArrayList<JingXuanData> getJingXuanData(String Url) {
		ArrayList<JingXuanData> list = new ArrayList<JingXuanData>();
		JSONObject json = ParseJson(Url, "utf-8");
		Log.i("","---->"+json.toString());
		
		if (json == null) {
			return null;
		} else {
			try {
				
				JSONArray Data = json.getJSONObject("obj").getJSONArray("list");
				for (int i = 0; i <Data.length() ;i++) {
					JSONObject data = Data.getJSONObject(i);
					JingXuanData jingXuanData =new JingXuanData();
					jingXuanData.setId(data.getString("id"));
					JSONObject element =data.getJSONObject("tour");
					jingXuanData.setTitle(element.getString("title"));
					jingXuanData.setPubdate(element.getString("startdate"));
					jingXuanData.setPictureCount(element.getString("cntP"));
					jingXuanData.setImage(element.getString("coverpic"));
					jingXuanData.setViewCount(element.getString("pcolor"));
					jingXuanData.setFavoriteCount(element.getString("likeCnt"));
					jingXuanData.setViewCount(element.getString("viewCnt"));
					UserInfo userInfo =new UserInfo();
					JSONObject owner =element.getJSONObject("owner");
					userInfo.setUsername(owner.getString("username"));
					userInfo.setNickname(owner.getString("nickname"));
					userInfo.setUserId(owner.getString("userid"));
					userInfo.setAvatar(owner.getString("avatar"));
					jingXuanData.setUserInfo(userInfo);
					JSONArray dispcitys =element.getJSONArray("dispCities");
					String[] citys =new String[dispcitys.length()];
					for (int j = 0; j < dispcitys.length(); j++) {
						
						citys[j]=dispcitys.getString(j);
					}
					jingXuanData.setDispCities(citys);
					//System.out.println("----->"+jingXuanData.getDispCities().length);
					
					list.add(jingXuanData);
					
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return list;

		}

	}
}
